package org.bigjava.buff.goods.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bigjava.buff.goods.bean.Type;
import org.bigjava.buff.goods.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/type")
public class TypeController {

	@Autowired
	private TypeService typeService;

	private Integer fid = null;

	//查询所有一级分类
	@RequestMapping("/list")
	public ModelAndView typeList() throws Exception{
		List<Type> list = typeService.findtypeByfid(0);
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("typelist", list);
		modelAndView.setViewName("adminpage/typelist");
		return modelAndView;
	}

	//添加一级分类
	@RequestMapping("/addfirst")
	public String addfist(String typeName) throws Exception{
		Type ttype = new Type();
		ttype.setTypeName(typeName);
		//一级分类fid默认为0
		ttype.setFid(0);
		typeService.addfirst(ttype);
		
		return "redirect:/type/list";
	}

	//批量删除一级分类
	@RequestMapping("/delAll")
	public String delAll(String[] ids) throws Exception{
		List<Integer> secondList = new ArrayList();
		//通过一级分类的id获取所有二级分类id
		for(String id : ids){
			List<Type> tmp = typeService.findtypeByfid(Integer.parseInt(id));
			if(tmp != null){
				for(Type t: tmp){
					secondList.add(t.getId());
				}
			}
		}
		//删除所有一级分类
		if(secondList != null){
			for(int id : secondList){
				typeService.delAll(id);
			}
		}
		//删除一级分类之下的二级分类
		for(String string : ids){
			typeService.delAll(Integer.parseInt(string));
		}
		
		return "redirect:/type/list";
	}

	//查询所有二级分类
	@RequestMapping("/typeEdit/{id}")
	public ModelAndView TypeEdit(@PathVariable("id") Integer id) throws Exception{
		fid = id;
		//通过一级分类id查询拥有相同fid的所有二级分类
		List<Type> list = typeService.findtypeByfid(id);
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("reclassifylist", list);
		modelAndView.setViewName("adminpage/reclassify");
		return modelAndView;
	}

	//跳转至后台主页面
	@RequestMapping("/BuffTerminal")
	public String reclassify() throws Exception{
		return "adminpage/BuffTerminal";
	}

	//添加二级分类
	@RequestMapping("addreclassify")
	public String addreclassify(String typeName) throws Exception{
		Type ttype = new Type();
		ttype.setTypeName(typeName);
		//二级分类的fid为一级分类id
		ttype.setFid(fid);
		typeService.addfirst(ttype);
		return "redirect:/type/typeEdit/"+fid;
	}

	//二级分类批量删除
	@RequestMapping("/delreclassify")

	//删除选中的分类
	public String delreclassify(String[] ids) throws Exception{
		//使用加强for循环删除数组中的id对象
		for(String string : ids){
			typeService.delAll(Integer.parseInt(string));
		}
		return "redirect:/type/typeEdit/"+fid;
	}

	//异步请求来通过fid获得名称集合
	@RequestMapping("/getName")
	@ResponseBody
	public String getName(Integer fid, HttpServletRequest request, String type) throws Exception {

		System.err.println("=================================" + type);

		List list;
		if(fid != null) {
			 list = typeService.findtypeByfid(fid);
		} else {
			 list = typeService.getTypeByTypeName(type);
			 request.setAttribute("liList", list);
		}

		ObjectMapper objectMapper = new ObjectMapper();
		String data = objectMapper.writeValueAsString(list);

		return data;
	}

}
